package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ProcurementBean;
import com.project.bean.ShopBean;
import com.project.mapper.IProcurementInfoMapper;
import com.project.mapper.IProcurementMapper;
import com.project.service.IProcurementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Map;

/**
 * 采购业务接口实现类
 */
@Service
@Transactional
public class ProcurementServiceImpl implements IProcurementService {
    @Autowired
    private IProcurementMapper procurementMapper;
    @Autowired
    private IProcurementInfoMapper procurementInfoMapper;
    @Override
    public void addRespository(ProcurementBean procurement, Map<String, ShopBean> buyMap) {
        //添加采购信息
        procurementMapper.insert(procurement);

        //添加采购明细
        procurementMapper.addProcurementInfo(procurement.getId(),buyMap.values());
        //清空采购车
        buyMap.clear();
    }

    @Override
    public IPage<ProcurementBean> findByItem(Integer pageNO, LocalDate startDate, LocalDate endDate) {
        QueryWrapper<ProcurementBean> qw = new QueryWrapper<>();
        if(startDate != null){
            qw.ge("p_buyDate",startDate);
        }
        if(endDate != null){
            qw.le("p_buyDate",endDate);
        }

        return procurementMapper.selectPage(new Page(pageNO,3),qw);
    }

    @Override
    public ProcurementBean findById(Integer id) {
        ProcurementBean product = procurementMapper.selectById(id);
        product.setInfoList(procurementInfoMapper.findByProcurement(id));
        return product;
    }
}
